﻿using System;
using System.Web.UI.WebControls;
using Cms.Data;
using Cms.Domain.RepositoryInterfaces;
using Cms.Utilities;
using Cms.Web.Framework.Plugins.Attributes;
using Content = Cms.Domain.Content;

namespace Cms.Web.Framework.Plugins
{
    public class ShowContent : BaseTemplatedPlugin
    {
        // Property fields
        private int _contentID;

        // Template controls
        private Panel pnlContent;
        
        [Browse("content")]
        public int ContentID
        {
            get { return _contentID; }
            set { _contentID = value; }
        }
        
        protected override void CreateChildControls()
        {
            try
            {
                base.CreateChildControls();
                pnlContent = (Panel)FindControl("pnlContent");
                IContentRepository contentRepository = new ContentRepository();
                Content content = contentRepository.GetContent(_contentID);
                ContentParser.Parse(content.GetPublishedHtml(RequestDetails.Locale.ID), pnlContent, RequestDetails.RequestedUrl);
            }
            catch (Exception ex)
            {
                Logging.LogError("ShowContent", ex);
                Controls.Clear();
            }
        }
    }
}